package com.app.controller;

import com.app.entity.BackendUser;
import com.app.entity.DevUser;
import com.app.service.DevService;
import com.app.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;


    @RequestMapping("/bklogin")
    public String backendLogin() {
        return "backendlogin";
    }

    @RequestMapping("dologin")
    public String doBackendLogin(@RequestParam("userCode") String userCode,
                                 @RequestParam("userPassword") String userPassword,
                                 HttpSession session,
                                 Model model) {
        //调用service的验证方法
        BackendUser login = loginService.login(userCode, userPassword);
        //验证登录
        if (login != null) {
            //登陆成功保存session
            session.setAttribute("login", login);
            return "redirect:/backend/main";
        } else {
            //登陆失败回显用户名
            model.addAttribute("userCode", userCode);
            model.addAttribute("msg", "用户名或密码错误");
            return "backendlogin";
        }
    }
    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("login");
        return "redirect:bklogin";
    }


    @Autowired
    private DevService devService;
    @RequestMapping("/devlogin")
    public String devlogin(){
        return "devlogin";
    }
    @RequestMapping("/dlogin")
    public String doDevLogin(@RequestParam("devCode") String devCode,
                             @RequestParam("devPassword") String devPassword,
                             HttpSession session,
                             Model model){
        //调用service的验证方法
        DevUser login = devService.devLogin(devCode, devPassword);
        //验证登录
        if(login != null){
            //登陆成功保存session
            session.setAttribute("devlogin", login);
            return "redirect:/developer/main";
        }else{
            //登陆失败回显用户名
            model.addAttribute("devCode", devCode);
            model.addAttribute("msg", "用户名或密码错误");
            return "devlogin";
        }
    }
    @RequestMapping("/devlogout")
    public String devlogout(HttpSession session){
        session.removeAttribute("devlogin");
        return "redirect:devlogin";
    }

}
